package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.icanci.loopstack.bic.biz.dts.GraphDtsServiceProxy;
import cn.icanci.loopstack.bic.biz.mapper.NodeMapper;
import cn.icanci.loopstack.bic.biz.service.NodeService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.NodeVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.NodeDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.NodeDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/11 22:57
 */
@Service("nodeService")
public class NodeServiceImpl implements NodeService {
    @Resource
    private NodeDAO              nodeDAO;
    @Resource
    private NodeMapper           nodeMapper;
    @Resource
    private GraphDtsServiceProxy graphDtsServiceProxy;

    @Override
    public List<NodeVO> queryAll() {
        return nodeMapper.dos2vos(nodeDAO.queryAll());
    }

    @Override
    //    @Transactional(rollbackFor = Exception.class)
    public void save(NodeVO nodeVO) {
        // TODO 日志记录
        // TODO 事务插入
        if (doInsert(nodeVO)) {
            NodeDO insert = nodeMapper.vo2do(nodeVO);
            nodeDAO.insert(insert);
             graphDtsServiceProxy.dtsUnitViewTree(nodeMapper.do2vo(insert));
        } else {
            nodeDAO.update(nodeMapper.vo2do(nodeVO));
        }
    }

    @Override
    public NodeVO queryById(String id) {
        return nodeMapper.do2vo(nodeDAO.queryOneById(id));
    }

    @Override
    public PageList<NodeVO> queryPage(NodeVO nodeVO, int pageNum, int pageSize) {
        PageList<NodeDO> pageQuery = nodeDAO.pageQuery(nodeMapper.vo2do(nodeVO), pageNum, pageSize);
        return new PageList<>(nodeMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());
    }

    @Override
    public NodeVO queryBySystemUuidAndName(String systemUuid, String name) {
        return nodeMapper.do2vo(nodeDAO.queryBySystemUuidAndName(systemUuid, name));
    }

    @Override
    public NodeVO queryBySystemUuidAndKey(String systemUuid, String key) {
        return nodeMapper.do2vo(nodeDAO.queryBySystemUuidAndKey(systemUuid, key));
    }

    @Override
    @SuppressWarnings("all")
    public List<TextValue> loadSelector(String systemUuid) {
        List<NodeVO> nodes = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (NodeVO node : nodes) {
            if (!StringUtils.equals(systemUuid, node.getSystemUuid())) {
                continue;
            }

            String label;
            if (isDeleted(node)) {
                label = String.format(DELETED_FORMAT, node.getNodeName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, node.getNodeName());
            }
            String value = node.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public List<TextValue> loadAllSelector() {
        List<NodeVO> nodes = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (NodeVO node : nodes) {
            String label;
            if (isDeleted(node)) {
                label = String.format(DELETED_FORMAT, node.getNodeName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, node.getNodeName());
            }
            String value = node.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public NodeVO queryByUuid(String nodeUuid) {
        return nodeMapper.do2vo(nodeDAO.queryByUuid(nodeUuid));
    }
}
